/**
 *
 * @author Doan Tuan Anh
 */

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
            
public class GoldFish extends Sprite {
    
    private static final int TOTAL_FRAME = 5;
    boolean right;
    private int myY;
    int WIDTH, HEIGHT;
    private int leftBound;
    private int rightBound;
    
    public GoldFish(boolean right, int initialY) throws Exception {

       super(Image.createImage("/goldfish.png"), Image.createImage("/goldfish.png").getWidth() / TOTAL_FRAME, Image.createImage("/goldfish.png").getHeight());

       defineReferencePixel(super.getWidth() / 2, super.getHeight() / 2);

       WIDTH = Image.createImage("/goldfish.png").getWidth() / TOTAL_FRAME;
       HEIGHT = Image.createImage("/goldfish.png").getHeight();

       myY = initialY;
       this.right = right;
       if(!right) setTransform(TRANS_MIRROR);

       setVisible(false);
    }
        
    void checkCollision(GoldFish goldFish)
    {
        if(collidesWith(goldFish, true)) 
        {
            if(getRefPixelX() >= rightBound - 10 || getRefPixelX() <= leftBound + 10)
            {
                setVisible(false);
                return;
            }

            if(right != goldFish.right)
            {
                changeDirection(5);
                goldFish.changeDirection(5);
            }
            else
            {
                if(right)
                {
                    if(getRefPixelX() > goldFish.getRefPixelX()) setVisible(false);
                    else goldFish.setVisible(false);
                }
                else
                {
                    if(getRefPixelX() > goldFish.getRefPixelX()) goldFish.setVisible(false);
                    else setVisible(false);
                }
            }
        }
    }
        
    void changeDirection(int distX)
    {
        right = !right;

        if(!right)
        {
            setTransform(TRANS_MIRROR);
            move(distX, 0);
        }
        else
        {
            setTransform(TRANS_NONE);
            move(-distX, 0);
        }
    }
    
  synchronized boolean go(boolean right, int posY) {
    boolean retVal = false;
    if(!isVisible()) {
      retVal = true;
      myY = posY;
      this.right = right;
      setVisible(true);
  
      if(!right) setTransform(TRANS_MIRROR);
      else setTransform(TRANS_NONE);

      if(right) {
	setRefPixelPosition(rightBound, myY);
	move(-1, 0);
      } else { //right
	setRefPixelPosition(leftBound, myY);
	move(1, 0);
      }
    } else {

    }
    return(retVal);
  }

  void reset() {
    setVisible(false);
  }
  
  synchronized int advance(Duck duck, int tickCount,
	      int currentLeftBound, int currentRightBound) {
    int retVal = 0;
    leftBound = currentLeftBound;
    rightBound = currentRightBound;
    
    if((getRefPixelX() - WIDTH >= rightBound) && (!right)) {
      setVisible(false);
    } 
    if((getRefPixelX() + WIDTH <= leftBound) && right) {
      setVisible(false);
    } 
    if(isVisible()) {
       nextFrame();
      if(right) {
	move(-4, 0);
      } else {
	move(4, 0);
      }
    }
    return(retVal);
  }
}
